package com.xjlsoft.weather.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xjlsoft.weather.enums.ResultCodeEnum;
import com.xjlsoft.weather.exception.WeatherException;

public class FileUtil {

	private static int deleteSumFile=0;
	private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
	/**
	 * 复制文件 
	 * 成功返回 true
	 * 复制失败 则抛出  复制文件异常
	 * */
	public static void copyFile(File source, File dest) throws Exception{
		
		FileUtils.copyFile(source, dest);
		
		if(!dest.exists()){
			throw new WeatherException(ResultCodeEnum.ERROR_9005);
		}
		logger.info("复制成功");
		
	}

	/**
	 * 根据文件路径 返回文件后缀
	 * 
	 * 无法获取到 则返回空字符串
	 * 
	 */
	public static String getfileType(String filepath) {
		int ty = filepath.lastIndexOf(".");
		String fileType = "";
		if (ty != -1) {
			fileType = filepath.substring(ty + 1);
		}
		return fileType;
	}

	/**
	 * 
	 * 获取 文件路径 去除后缀
	 * 
	 * 例如 输入 2019_05_13/97e7046c74414b4e8a8ff3822d9ff99f.docx 输出
	 * 2019_05_13/97e7046c74414b4e8a8ff3822d9ff99f
	 * 
	 */
	public static String getfilePath(String filepath) {
		int ty = filepath.lastIndexOf(".");
		String fileType = "";
		if (ty != -1) {
			fileType = filepath.substring(0, ty);
		}
		return fileType;
	}

	/**
	 * @Description ：简单判断是否是office文件
	 * @author : lj
	 * @param :
	 *            [fileType]
	 * @return : boolean
	 * @exception :
	 */
	public static boolean isOfficeFile(String fileType) {
		fileType = fileType.toLowerCase();
		System.out.println(fileType);
		switch (fileType) {
		case "txt":
		case "doc":
		case "docx":
		case "xls":
		case "xlsx":
		case "ppt":
		case "pptx":
		case "pdf":
		case "html":
		case "xml":
			return true;
		}
		return false;
	}

	/**
	 * @Description : TXT文件编码转换，解决转pdf中文乱码问题
	 * @author : lj
	 * @param :
	 *            [file]
	 * @return : boolean
	 * @exception :
	 */
	public static boolean tranTxt(File file) {
		try {
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis, "GBK");
			BufferedReader br = new BufferedReader(isr);
			String OriStr;
			StringBuilder stringBuilder = new StringBuilder();
			while ((OriStr = br.readLine()) != null) {
				// 手动拼接换行符
				OriStr += "\n";
				stringBuilder.append(OriStr);
			}
			String targetStr = stringBuilder.toString();
			// false代表不追加直接覆盖,true代表追加文件
			FileOutputStream fos = new FileOutputStream(file, false);
			OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
			osw.write(stringBuilder.toString());
			osw.flush();
			osw.close();
			fos.close();
			br.close();
			isr.close();
			fis.close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
	
	  //需要注意的是当删除某一目录时，必须保证该目录下没有其他文件才能正确删除，否则将删除失败。
	/**
	 * 
	 * 删除目录下的所有文件  目录删除
	 * @return  返回删除文件数量
	 * */
    public static int deleteFolder(File folder) throws Exception {
    	
        if (!folder.exists()) {
            throw new Exception("文件不存在");
        }
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    //递归直到目录下没有文件
                    deleteFolder(file);
                } else {
                    //删除
                    file.delete();
                    deleteSumFile++;
                }
            }
        }
        return deleteSumFile;
        //删除
       // folder.delete();

    }


}
